All files / src/components/admin CreateBanModal.tsx

0% Statements 0/54
0% Branches 0/22
0% Functions 0/9
0% Lines 0/51

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210                                                                                                                                                                                                                                                                                                                                                                                                                                   
import React, { useState } from 'react';
import { SecurityService } from '../../services/securityService';
import { BAN_TYPES, BAN_LEVELS } from '../../types/security';
import { toast } from 'react-hot-toast';
import { useTranslation } from 'react-i18next';
import useLoadNamespace from '@/hooks/useLoadNamespace';
 
interface CreateBanModalProps {
  isOpen: boolean;
  onClose: () => void;
  onSuccess: () => void;
}
 
const CreateBanModal: React.FC<CreateBanModalProps> = ({ isOpen, onClose, onSuccess }) => {
  useLoadNamespace('admin/security');
  const { t } = useTranslation(['admin/security', 'translation']);
  const [formData, setFormData] = useState({
    username: '',
    ban_type: BAN_TYPES.TEMPORARY,
    ban_reason: '',
    ban_level: BAN_LEVELS.FIRST,
    custom_duration: ''});
  const [loading, setLoading] = useState(false);
 
  const handleSubmit = async (e: React.FormEvent) => {
    e.preventDefault();
    
    if (!formData.username.trim() || !formData.ban_reason.trim()) {
      toast.error(t('notifications.ban.usernameAndReasonRequired'));
      return;
    }
 
    try {
      setLoading(true);
 
      let expires_at: string | undefined;
      
      if (formData.ban_type === BAN_TYPES.TEMPORARY) {
          if (formData.custom_duration) {
          // Custom duration in hours
          const hours = parseInt(formData.custom_duration);
          if (isNaN(hours) || hours <= 0) {
            toast.error(t('notifications.ban.invalidDuration'));
            return;
          }
          const expiryDate = new Date();
          expiryDate.setHours(expiryDate.getHours() + hours);
          expires_at = expiryDate.toISOString();
        } else {
          // Use predefined durations based on ban level
          const expiryDate = new Date();
          if (formData.ban_level === BAN_LEVELS.FIRST) {
            expiryDate.setMinutes(expiryDate.getMinutes() + 20);
            expires_at = expiryDate.toISOString();
          } else if (formData.ban_level === BAN_LEVELS.SECOND) {
            expiryDate.setHours(expiryDate.getHours() + 72);
            expires_at = expiryDate.toISOString();
          } else {
            expires_at = undefined; // Permanent
          }
        }
      }
 
      await SecurityService.createSecurityBan({
        username: formData.username.trim(),
        ban_type: formData.ban_type,
        ban_reason: formData.ban_reason.trim(),
        ban_level: formData.ban_level,
        expires_at});
 
      toast.success(t('notifications.ban.userBanned', { user: formData.username }));
      onSuccess();
      
      // Reset form
      setFormData({
        username: '',
        ban_type: BAN_TYPES.TEMPORARY,
        ban_reason: '',
        ban_level: BAN_LEVELS.FIRST,
        custom_duration: ''});
    } catch (error) {
      console.error('Error creating ban:', error);
      toast.error(t('notifications.ban.errorCreatingBan'));
    } finally {
      setLoading(false);
    }
  };
 
  const handleInputChange = (field: string, value: string | number | boolean) => {
    setFormData(prev => ({ ...prev, [field]: value }));
  };
 
  if (!isOpen) return null;
 
  return (
    <div className="fixed inset-0 bg-gray-600 bg-opacity-50 overflow-y-auto h-full w-full z-50">
      <div className="relative top-20 mx-auto p-5 border w-96 shadow-lg rounded-md bg-white">
        <div className="mt-3">
          <h3 className="text-lg font-medium text-gray-900 mb-4">{t('admin.createBan.title')}</h3>
          
          <form onSubmit={handleSubmit} className="space-y-4">
            {/* Username */}
            <div>
              <label className="block text-sm font-medium text-gray-700 mb-1">
                {t('admin.createBan.usernameLabel')}
              </label>
              <input
                type="text"
                value={formData.username}
                onChange={(e) => handleInputChange('username', e.target.value)}
                className="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500"
                placeholder={t('admin.createBan.usernamePlaceholder')}
                required
              />
            </div>
 
            {/* Ban Type */}
            <div>
              <label className="block text-sm font-medium text-gray-700 mb-1">
                {t('admin.createBan.banTypeLabel')}
              </label>
              <select
                value={formData.ban_type}
                onChange={(e) => handleInputChange('ban_type', e.target.value)}
                className="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500"
              >
                <option value={BAN_TYPES.TEMPORARY}>{t('admin.createBan.temporary')}</option>
                <option value={BAN_TYPES.PERMANENT}>{t('admin.createBan.permanent')}</option>
              </select>
            </div>
 
            {/* Ban Level (for temporary bans) */}
            {formData.ban_type === BAN_TYPES.TEMPORARY && (
              <div>
                <label className="block text-sm font-medium text-gray-700 mb-1">
                  {t('admin.createBan.banLevelLabel')}
                </label>
                <select
                  value={formData.ban_level}
                  onChange={(e) => handleInputChange('ban_level', parseInt(e.target.value))}
                  className="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500"
                >
                  <option value={BAN_LEVELS.FIRST}>{t('admin.createBan.level1')}</option>
                  <option value={BAN_LEVELS.SECOND}>{t('admin.createBan.level2')}</option>
                  <option value={BAN_LEVELS.PERMANENT}>{t('admin.createBan.level3')}</option>
                </select>
              </div>
            )}
 
            {/* Custom Duration (for temporary bans) */}
            {formData.ban_type === BAN_TYPES.TEMPORARY && (
              <div>
                <label className="block text-sm font-medium text-gray-700 mb-1">
                  {t('admin.createBan.customDurationLabel')}
                </label>
                <input
                  type="number"
                  value={formData.custom_duration}
                  onChange={(e) => handleInputChange('custom_duration', e.target.value)}
                  className="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500"
                  placeholder={t('admin.createBan.customDurationPlaceholder')}
                  min="1"
                />
                <p className="text-xs text-gray-500 mt-1">
                  {t('admin.createBan.customDurationNote')}
                </p>
              </div>
            )}
 
            {/* Ban Reason */}
            <div>
              <label className="block text-sm font-medium text-gray-700 mb-1">
                {t('admin.createBan.reasonLabel')}
              </label>
              <textarea
                value={formData.ban_reason}
                onChange={(e) => handleInputChange('ban_reason', e.target.value)}
                className="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500"
                rows={3}
                placeholder={t('admin.createBan.reasonPlaceholder')}
                required
              />
            </div>
 
            {/* Buttons */}
            <div className="flex items-center justify-end space-x-3 pt-4">
              <button
                type="button"
                onClick={onClose}
                className="px-4 py-2 border border-gray-300 rounded-md shadow-sm text-sm font-medium text-gray-700 bg-white hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500"
              >
                {t('common.cancel')}
              </button>
              <button
                type="submit"
                disabled={loading}
                className="px-4 py-2 border border-transparent rounded-md shadow-sm text-sm font-medium text-white bg-red-600 hover:bg-red-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-red-500 disabled:opacity-50 disabled:cursor-not-allowed"
              >
                {loading ? t('common.loading', { ns: 'translation' }) : t('common.create', { ns: 'translation' })}
              </button>
            </div>
          </form>
        </div>
      </div>
    </div>
  );
};
 
export default CreateBanModal;